/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  FunCall
 * 
 * Description of class:
 * 
 * Concrete implementation of LangObject
 * 
 */

package karel.lang.secondpass;

import java.io.IOException;
import karel.lang.RDParser;
import karel.lang.RDParserException;
import karel.lang.newParserTools.RDBuiltIn;


public class FunCall extends LangObject {
    
    private String name;
    private int nrOfArguments;
    private int ar;
    
    public FunCall(String name, int nrOfArguments, int ar, int line, int column) {
        super(line, column);
        this.name = name;
        this.nrOfArguments = nrOfArguments;
        this.ar = ar;
    }

    @Override
    public void generate(RDParser parser) throws IOException {
        if(buildInFuncFound()) {
            if(name.equals("!")) {
                /*
                parser.asmCode.append("(Not)\n");
                return;
                */
                name = "NOT";
            }
            parser.asmCode.append("(Call #\"").append(name).append("[f").append(nrOfArguments).append("]\" ").append(ar).append(")\n");
            return;
        } 
        Program.SearchResults res = parent.find(name, nrOfArguments);
        if(res != null) {
            parser.asmCode.append("(Fetch ").append(res.level).append(" ").append(res.pos).append(")\n");
            parser.asmCode.append("(CallClosure ").append(nrOfArguments).append(" ").append(ar).append(")\n");
        }
        else {
            throw new RDParserException("Stef að nafni '"+name+"' með "+nrOfArguments+" "+(nrOfArguments>1 ? "viðföng" : "viðfang")+" ekki skilgreint!",line,column,name.length());
        }
    }
    
    private boolean buildInFuncFound() {
        for(RDBuiltIn.Function func: RDBuiltIn.functions) {
            if(func.name.equals(name) && func.nrOfargs == nrOfArguments) {
                return true;
            }
        }
        return false;
    }

}
